package program.queue;

import program.array.ArrayV2;

/**
 * 使用{@link ArrayV2} 作为顶层数据结构.
 * 存在的问题: 因为是数组, 所以出队一个元素, 就需要移动整个数组的后续元素; 出队的复杂度为O(n);
 */
public class ArrayQueue<E> implements Queue<E> {

    private ArrayV2<E> data;

    public ArrayQueue(int capacity) {
        data = new ArrayV2<>(capacity);
    }

    public ArrayQueue() {
        data = new ArrayV2<>();
    }

    @Override
    public int getSize() {
        return data.getSize();
    }

    @Override
    public boolean isEmpty() {
        return data.isEmpty();
    }

    @Override
    public void enqueue(E e) {
        data.addLast(e);
    }

    @Override
    public E dequeue() {
        return data.removeFirst();
    }

    @Override
    public E getFront() {
        return data.getFirst();
    }

    @Override
    public String toString() {
        StringBuilder resultStr = new StringBuilder();
        int size = data.getSize();
        resultStr.append(String.format("Queue size:%d, capacity: %d\n", size, data.getCapacity()));
        resultStr.append("front [");
        for (int i = 0; i < size; i++) {
            resultStr.append(data.get(i));
            if (i != size - 1) {
                resultStr.append(", ");
            }
        }

        resultStr.append("]tail");
        return resultStr.toString();
    }
}
